package com.gardenguell.salad.ui.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import com.gardenguell.salad.R;
import com.gardenguell.salad.util.CommonLog;
import com.gardenguell.salad.util.DisplayUtil;

/**
 * Created by absurd on 2015/12/22.
 */
public class TagSeekBar extends LinearLayout {

    private static final int OFFSET = 8;

    private TextView mAttrName;
    private TextView mStartTag;
    private TextView mEndTag;
    private TextView mCurTag;
    private SeekBar mSeekBar;

    private float mStart;
    private float mEnd;
    private float mCur;



    public TagSeekBar(final Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.tag_seek_bar, this, true);
        mAttrName = (TextView)findViewById(R.id.tv_attr_name);
        mStartTag = (TextView)findViewById(R.id.tv_start_tag);
        mEndTag = (TextView)findViewById(R.id.tv_end_tag);
        mCurTag = (TextView)findViewById(R.id.tv_cur_tag);
        mSeekBar = (SeekBar)findViewById(R.id.sb_seekbar);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TagSeekBar);
        for(int i = 0; i < ta.getIndexCount(); i++){
            int attr = ta.getIndex(i);
            switch(attr){
                case R.styleable.TagSeekBar_attr:
                    mAttrName.setText(ta.getString(attr));
                    break;
                case R.styleable.TagSeekBar_cur:
                    mCur = ta.getFloat(attr, 0f);
                    mCurTag.setText(mCur + "");
                    break;
                case R.styleable.TagSeekBar_start:
                    mStart = ta.getFloat(attr, 0f);
                    mStartTag.setText(mStart + "");
                    break;
                case R.styleable.TagSeekBar_end:
                    mEnd = ta.getFloat(attr, 0f);
                    mEndTag.setText(mEnd + "");
                    break;
            }
        }
        ta.recycle();

        mSeekBar.setProgress((int)(mCur - mStart));
        mSeekBar.setMax((int)(mEnd - mStart));
        // get the width of the seekbar
        ViewTreeObserver vto = mSeekBar.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onGlobalLayout() {
                mSeekBar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                invalidCurTag(context, mCur - mStart);
            }
        });

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                invalidCurTag(context, progress);
                mCurTag.setText((progress + mStart) + "");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });



    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    public void setCur(float cur){
        mSeekBar.setProgress((int)(cur - mStart));
        mCurTag.setText((int)cur + "");
    }

    public void invalidCurTag(Context context, float progress){
        LinearLayout.LayoutParams curTagParams = (LinearLayout.LayoutParams)mCurTag.getLayoutParams();
        int width = mSeekBar.getWidth() - mCurTag.getWidth() - DisplayUtil.dip2px(context,2 * OFFSET);
        curTagParams.leftMargin = (int)(progress / (mEnd - mStart) * width + DisplayUtil.dip2px(context,OFFSET));
        mCurTag.setLayoutParams(curTagParams);
    }

    public TagSeekBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TagSeekBar(Context context) {
        this(context, null);
    }

    public void setStartTag(float start){
        mStartTag.setText(start + "");
    }

    public void setmEndTag(float end){
        mEndTag.setText(end + "");
    }

    public void setCurTag(int cur){
        mCurTag.setText(cur + "");
    }

    public float getCur(){
        return mSeekBar.getProgress() + mStart;
    }

    public float getStart(){
        return mStart;
    }

    public float getEnd(){
        return mEnd;
    }



}
